package com.jsgt.greekfire.common.websocket

import android.text.TextUtils
import com.fhx.core.utils.LogUtils.error
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.jsgt.greekfire.bean.GFTKLineData
import com.jsgt.greekfire.bean.NewPrice
import com.jsgt.greekfire.bean.TickMarket
import org.java_websocket.client.WebSocketClient
import org.java_websocket.drafts.Draft_6455
import org.java_websocket.handshake.ServerHandshake
import java.net.URI
import java.util.*

class MyWebSocketClient(serverUri: URI?) :
    WebSocketClient(serverUri, Draft_6455()) {
    private val onSocketListeners: MutableList<OnSocketListener> =
        ArrayList()

    override fun onOpen(handshakedata: ServerHandshake) {
        error("onOpen()")
        for (onSocketListener in onSocketListeners) {
            onSocketListener.onOpen(handshakedata)
        }
    }

    override fun onMessage(message: String) {
        error("onMessage : $message")
        var message1: Message<GFTKLineData?>? = null
        var message2: Message<NewPrice?>? = null
        var message3: Message<TickMarket?>? = null
        if (message.isNotEmpty() && message.contains(Message.TYPE_KLINE)) {
            message1 = Gson().fromJson(
                message,
                object :
                    TypeToken<Message<GFTKLineData?>?>() {}.type
            )
        } else if (!TextUtils.isEmpty(message) && message.contains(Message.TYPE_MARKET)) {
            message2 = Gson().fromJson(
                message,
                object :
                    TypeToken<Message<NewPrice?>?>() {}.type
            )
        } else if (!TextUtils.isEmpty(message) && message.contains(Message.TYPE_TICK)) {
            message3 = Gson().fromJson(
                message,
                object :
                    TypeToken<Message<TickMarket?>?>() {}.type
            )
        }
        for (onSocketListener in onSocketListeners) {
            onSocketListener.onMessage(message1, message2, message3)
        }
    }

    override fun onClose(
        code: Int,
        reason: String,
        remote: Boolean
    ) {
        error("onClose()")
        for (onSocketListener in onSocketListeners) {
            onSocketListener.onClose(code, reason, remote)
        }
    }

    override fun onError(ex: Exception) {
        error("onError() : " + ex.message)
        for (onSocketListener in onSocketListeners) {
            onSocketListener.onError(ex)
        }
    }

    fun addOnSocketListener(onSocketListener: OnSocketListener?) {
        if (onSocketListener != null) {
            onSocketListeners.add(onSocketListener)
        }
    }

    fun removeOnSocketListener(onSocketListener: OnSocketListener?) {
        if (onSocketListener != null) {
            onSocketListeners.remove(onSocketListener)
        }
    }

    fun hasListener(): Boolean {
        return onSocketListeners.size > 0
    }

    interface OnSocketListener {
        fun onOpen(handshakedata: ServerHandshake?)
        fun onMessage(
            message1: Message<GFTKLineData?>?,
            message2: Message<NewPrice?>?,
            message3: Message<TickMarket?>?
        )

        fun onClose(
            code: Int,
            reason: String?,
            remote: Boolean
        )

        fun onError(ex: Exception?)
    }
}